package com.woniu.util;

import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.woniu.config.OSSProerties;

import lombok.Data;

@Component
@Data
public class AliyunOssTemplate {
	
	@Autowired
	private OSSProerties op;
	
	public String upload(String fileName,String bucketName,InputStream fileInputStream) {
		OSS ossClient = null;
		try {
			ossClient = new OSSClientBuilder().build("https://"+op.getEndPoint(), op.getAccessKeyId(), op.getAccessKey());
			ossClient.putObject(bucketName, fileName, fileInputStream);
			String url = "http://"+bucketName+"." + op.getEndPoint() + "/" + fileName;
			return url;
		} finally {
			if(ossClient != null) ossClient.shutdown();
		}
	}
	
	public boolean delete(String fileName,String bucketName) {
		OSS ossClient = null;
		try {
			ossClient = new OSSClientBuilder().build("https://"+op.getEndPoint(), op.getAccessKeyId(), op.getAccessKey());
			ossClient.deleteObject(bucketName, fileName);
			return true;
		} finally {
			if(ossClient != null) ossClient.shutdown();
		}
	}

}
